
package com.sec.android.app.myfiles.widget.listview.pinchview;

import android.animation.Animator;
import android.view.View;

import java.util.ArrayList;

/**
 * @hide
 */
public class PinchAnimationFactory {
    private ArrayList<PinchAnimator> mAnimators = new ArrayList<>();

    public boolean isEmpty() {
        return mAnimators.isEmpty();
    }

    public void clear() {
        for (PinchAnimator animator : mAnimators) {
            animator.removeUpdateListener(animator);
        }
        mAnimators.clear();
    }

    public ArrayList<Animator> getCurrentAnimationList() {
        ArrayList<Animator> aniList = new ArrayList<>();
        for (PinchAnimator animator : mAnimators) {
            aniList.add(animator);
        }
        return aniList;
    }


    private void createAnimation(int animation, View view, int duration, float start, float end, long startDelay) {
        PinchAnimator animator = findMatchAnimator(duration, startDelay);
        if (animator == null) {
            animator = new PinchAnimator();
            mAnimators.add(animator);
        }

        animator.addProperty(view, animation, start, end);
        animator.setDuration(duration);
        animator.setStartDelay(startDelay);
    }


    public void createTransXAnimation(View view, int duration, float start, float end) {
        createAnimation(PinchAnimator.ANI_TYPE_TRANS_X, view, duration, start, end, 0);
    }

    public void createTransYAnimation(View view, int duration, float start, float end) {
        createAnimation(PinchAnimator.ANI_TYPE_TRANS_Y, view, duration, start, end, 0);
    }

    public void createWidthAnimation(View view, int duration, float start, float end) {
        createAnimation(PinchAnimator.ANI_TYPE_WIDTH, view, duration, start, end, 0);
    }

    public void createHeightAnimation(View view, int duration, float start, float end) {
        createAnimation(PinchAnimator.ANI_TYPE_HEIGHT, view, duration, start, end, 0);
    }

    private PinchAnimator findMatchAnimator(long duration, long startDelay) {
        if (mAnimators.isEmpty())
            return null;

        for (PinchAnimator animator : mAnimators) {
            if (animator.getDuration() == duration && animator.getStartDelay() == startDelay) {
                return animator;
            }
        }

        return null;
    }
}
